/**
 * @cxn
 */


//时间粒度 年季月旬日 对应54321
let DateGrade = {
    year: 5, 
    season: 4,
    month: 3, 
    tenDays: 2, 
    day: 1,
}
// let nowYear = DateUtil.getNowDate(DateGrade.year);
// let _years = DateUtil.getDateList(DateGrade.year, nowYear, DateLength.year)

let DateList = {
    year: ['2010年', '2011年', '2012年', '2013年', '2014年', '2015年', '2016年', '2017年'],
    season: ['1季度', '2季度', '3季度', '4季度'],
    month: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
    tenDays: ['上旬', '中旬', '下旬'],
    day: ['1日', '2日', '3日', '4日', '5日', '6日', '7日', '8日', '9日', '10日', '11日', '12日', '13日', '14日', '15日', '16日', '17日', '18日', '19日', '20日', '21日', '22日', '23日', '24日', '25日', '26日', '27日', '28日', '29日', '30日', '31日'],
}

let DateLength = {
    year: 3, 
    season: 5,
    month: 12, 
    tenDays: 6, 
    day: 10,
    getDateLength(date_grade){
        let dg = parseInt(date_grade);
        if (dg == DateGrade.year){
            return this.year;
        } else if (dg == DateGrade.season){
            return this.season;
        } else if (dg  == DateGrade.month){
            return this.month;
        } else if (dg == DateGrade.tenDays){
            return this.tenDays;
        } else if (dg == DateGrade.day){
            return this.day;
        }
    }
}

let DateUtil = {
    getBeforeDate(date_grade, date_end, date_length){
        return this.getDateList(date_grade, date_end, date_length)[0];
    },
    getNowDate(date_grade){
        var date = new Date();
        var dg = parseInt(date_grade);

        if (dg == DateGrade.year){
            return date.getFullYear() + '';
        } else if (dg == DateGrade.season){
            return date.getFullYear() + '' + (parseInt(date.getMonth() / 3)  + 1);
        } else if (dg == DateGrade.month){
            return date.getFullYear() + ((date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1));
        } else if (dg == DateGrade.tenDays){
            return date.getFullYear() + ((date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1)) + (date.getDate() > 29 ? '3' : parseInt(date.getDate() / 10 + 1));
        } else if (dg == DateGrade.day){
            return date.getFullYear() + ((date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1)) + (date.getDate() < 10 ? '0' : '') + date.getDate();
        }
    },

    getDateListBySE(date_grade, date_start, date_end){
        let dg = parseInt(date_grade);
        var list = [];
        var lastdate = date_end;
        while(date_start != lastdate){
            if (dg == DateGrade.year){
                lastdate = this.getLastYear(lastdate);
            } else if (dg == DateGrade.season){
                lastdate = this.getLastSeason(lastdate);
            } else if (dg == DateGrade.month){
                lastdate = this.getLastMonth(lastdate);
            } else if (dg == DateGrade.tenDays){
                lastdate = this.getLastTenDays(lastdate);
            } else if (dg == DateGrade.day){
                lastdate = this.getLastDay(lastdate);
            }
            list.unshift(lastdate + '');
        }
        list.push(date_end);
        return list;
    },

    getDateList(date_grade, date_end, date_length){
        let dg = parseInt(date_grade);
        var list = [];
        var lastdate = date_end;
        for (var i = date_length - 2; i >= 0 ; i--){
            if (dg == DateGrade.year){
                lastdate = this.getLastYear(lastdate);
            } else if (dg == DateGrade.season){
                lastdate = this.getLastSeason(lastdate);
            } else if (dg == DateGrade.month){
                lastdate = this.getLastMonth(lastdate);
            } else if (dg == DateGrade.tenDays){
                lastdate = this.getLastTenDays(lastdate);
            } else if (dg == DateGrade.day){
                lastdate = this.getLastDay(lastdate);
            }
            list[i] = lastdate + '';
        }
        list.push(date_end);
        return list;
    },

    //取上日yyyyMMdd
    getLastDay(yyyyMMdd){
        var year = yyyyMMdd.substr(0, 4);
        var month = yyyyMMdd.substr(4, 2);
        var day = yyyyMMdd.substr(6, 2);

        var d = new Date();
        d.setFullYear(parseInt(year));
        d.setMonth(parseInt(month) - 1);
        d.setDate(day);
        d.setDate(d.getDate() - 1);
        return d.getFullYear() + ((d.getMonth() < 9 ? '0' : '') + (d.getMonth() + 1)) + (d.getDate() < 10 ? '0' : '') + d.getDate();
    },

    //取上旬yyyyMMT
    getLastTenDays(yyyyS){
        var yyyyMM = yyyyS.substr(0, 6);
        var tenDays = yyyyS.substr(6, 1);
        if (parseInt(tenDays) == 1){
            return this.getLastMonth(yyyyMM) + '3';
        }else{
            return yyyyMM + (parseInt(tenDays) - 1);
        }
    },

    //取上月yyyyMM
    getLastMonth(yyyyMM){
        var year = yyyyMM.substr(0, 4);
        var month = yyyyMM.substr(4, 2);
        if (parseInt(month) > 12 || parseInt(month) < 1){
            return null;
        }

        var d = new Date();
        d.setFullYear(parseInt(year));
        d.setMonth(parseInt(month) - 1);
        d.setMonth(d.getMonth() - 1);
        return d.getFullYear() + ((d.getMonth() < 9 ? '0' : '') + (d.getMonth() + 1));
    },

    //取上季度yyyyS
    getLastSeason(yyyyS){
        var year = yyyyS.substr(0, 4);
        var season = yyyyS.substr(4);
        if (parseInt(season) == 1){
            return this.getLastYear(year) + '4';
        }else{
            return year + (parseInt(season) - 1);
        }
    },

    //取上年yyyy
    getLastYear(year){
        return (parseInt(year) - 1) + '';
    }

}

global.DateGrade = DateGrade;
global.DateList = DateList;
global.DateLength = DateLength;

global.DateUtil = DateUtil;